package sw6.digipecs.widget;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class AddImageAdapter extends BaseAdapter {
	private Context mContext; 
	private ArrayList<Integer>mList;
	private Activity mActivity; 
	public AddImageAdapter(Context context,Activity activity)
	{
		mContext=context; 
		mActivity = activity; 
		init(); 
	}

	@Override
	public int getCount() {
		return mList.size(); 
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position); 
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        Bitmap bmp = (MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(),
    	        mList.get(position), MediaStore.Images.Thumbnails.MICRO_KIND, null)); 
        imageView.setImageBitmap(bmp); 
        return imageView;
	}
	public void init()
	{
		String[] projection = { MediaStore.Images.Media._ID };
        String selection = ""; 
        String[] selectionArgs = null; 
        Cursor mImageCursor =  mActivity.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        		projection,selection,selectionArgs,null);
        mImageCursor.moveToFirst(); 
        mList = new ArrayList<Integer>(); 
        do{
        	int imageID = mImageCursor.getInt(mImageCursor.getColumnIndex(MediaStore.Images.Media._ID) );
        	//Uri uri = Uri.withAppendedPath( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
        	//                                Integer.toString(imageID) );
        	
        	
        	mList.add(imageID); 
        }
        while(mImageCursor.moveToNext()); 
	}
	
}
